package org.bp.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.UUID;

/**
 * @author baiping
 * @version 1.0
 * @date 2017/9/6
 */
public class StringUtils {
	// private static Log log = LogFactory.getLog(StringUtils.class);
	
	/**
	 * 获取len长度的汉字
	 * @param len
	 * @return
	 */
	public static String getHanZi(int len){
		StringBuilder ret = new StringBuilder();
		Random random = new Random();
		String str = null;
		byte[] b = new byte[2];
		int hightPos, lowPos; // 定义高低位
		for (int i = 0; i < len; i++) {
			hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
			lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
			b[0] = (new Integer(hightPos).byteValue());
			b[1] = (new Integer(lowPos).byteValue());
			
			try {
				str = new String(b, "GBk"); // 转成中文
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
			ret.append(str);
		}
		return ret.toString();
	}
	/**
	 * 首字母小写
	 * @param str
	 * @return
	 */
	public static String lowerHeader(String str){
		if(ObjectUtils.isNull(str)){
			return "";
		}
		return new StringBuilder(str.substring(0,1).toLowerCase()).append(str.substring(1)).toString();
	}

	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
	public static String upperHeader(String str){
		if(ObjectUtils.isNull(str)){
			return "";
		}
		return new StringBuilder(str.substring(0,1).toUpperCase()).append(str.substring(1)).toString();
	}
	/**
	 * 将null 转为 ""
	 * @param str
	 * @return
	 */
	public static String toString(String str){
		if(str == null)
			return "";
		if (str.equals("null"))
			return "";
		if (str.length() == 0)
			return "";
		return str.trim();
	}
	
	/**
     * 判断两个字符串是否想等
     * @param str   字符串参数
     * @param another  字符串参数
     * @return
     */
	public static boolean isEqual(String str, String another) {
		return ObjectUtils.isNull(str) ? ObjectUtils.isNull(another) : str.equals(another);
	}

	/**
	 * 解析 boolean 值，非0即为真
	 * @param str
	 * @return
	 */
	public boolean parserBoolean(String str) {
		if (ObjectUtils.isAbsNull(str)) {
			return false;
		}
		str = str.toLowerCase();
		return !(str.equals("false") || str.equals("0"));
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * 将strs追加到source
	 * 
	 * @param source
	 * @param strs
	 * @return
	 */
	public static String append(String source, String... strs) {
		StringBuilder stringBuilder = new StringBuilder(source);
		if (ObjectUtils.isNotNull(strs)) {
			for (String str : strs) {
				stringBuilder.append(str);
			}
		}
		return stringBuilder.toString();
	}

	/**
	 * getter方法名
	 * 
	 * @param fieldName
	 * @return
	 */
	public static String getterName(String fieldName) {
		if (fieldName != null && !"".equals(fieldName)) {
			fieldName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		}
		return fieldName;
	}

	/**
	 * setter方法名
	 * 
	 * @param fieldName
	 * @return
	 */
	public static String setterName(String fieldName) {
		if (fieldName != null && !"".equals(fieldName)) {
			fieldName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		}
		return fieldName;
	}

	/**
	 * 通过getter&setter方法名取得字段名
	 * 
	 * @param getterMethodName
	 * @return
	 */
	public static String fieldName(String getterMethodName) {
		if (ObjectUtils.isNotNull(getterMethodName) && getterMethodName.startsWith("get")) {
			return getterMethodName.substring(3, 4).toLowerCase() + getterMethodName.substring(4);
		}
		return getterMethodName;
	}
}
